home *** CD-ROM | disk | FTP | other *** search
- /*
- DU_LIB v2
- Gem Window Management & Dialog Library For Lattice C
- ½1994,95, by Craig Graham.
- Based on the DU_LIBv1 Library for HiSoft Basic.
-
- */
-
- #include "DULIB.H"
-
- #define DIALOG_LAYER_KEY_HANDLER -2
-
- /*
- EDITABLE TEXT OBJECTS
- */
-
- /*
- Make a text object edittable
- */
- void Set_object_editable(short dialog,short object)
- {
- OBJECT *t;
- TEDINFO *te;
-
- Set_object_Kcallback(dialog,object,&ted_callback);
- Set_object_callback(dialog,object,&ted_mouseclick);
- Set_object_redraw(dialog,object,&ted_display_cursor);
-
- rsrc_gaddr(0,dialog,&t);
- t+=object;
- te=(TEDINFO*)t->ob_spec;
-
- te->te_tmplen=0; // Use te_tmplen field to store cursor position instead
- dialog_details[dialog].current_focus=object; // Ensure we have a valid initial editable object
- Set_dialog_Kfocus_policy(dialog, FOCUS_FIELD);
- }
-
- /*
- Set the focus policy for a dialog
- Options are:
- FOCUS_MOUSE : key to object under mouse
- FOCUS_FIELD : key to object with keyboard focus
- */
- void Set_dialog_Kfocus_policy(short dialog, short focus_policy)
- {
- dialog_details[dialog].focus_mode=focus_policy;
- }
-
- /* Set the keyboard focus for a dialog, change highlight if dialog is open */
- void Set_dialog_Kfocus(short dialog, short object)
- {
- short n,wind,x,y,w,h,top_w;
- OBJECT *t;
- TEDINFO *te;
-
- rsrc_gaddr(0,dialog,&t);
-
- wind=0;
- for(n=0; (n<max_windows)&&(wind==0); n++)
- if ((windows[n].window_type!=wt_null)&&(windows[n].the_dialog==dialog))
- wind=n;
-
- if (wind)
- {
- wind_get(wind,WF_WORKXYWH,&x,&y,&w,&h);
- t->ob_x=x;
- t->ob_y=y;
- }
- wind_get(cr_wind_handle,WF_TOP,&top_w,0,0,0);
-
- graf_mouse(M_OFF,NULL);
- wind_update(BEG_UPDATE);
-
- if (dialog_details[dialog].current_focus)
- {
- te=(TEDINFO*)(t+dialog_details[dialog].current_focus)->ob_spec;
-
- te->te_thickness=1;
-
- if ((wind)&&(wind==top_w))
- objc_draw(t,dialog_details[dialog].current_focus,1,scrn_x,scrn_y,scrn_w,scrn_h);
- }
-
- dialog_details[dialog].current_focus=object;
- te=(TEDINFO*)(t+dialog_details[dialog].current_focus)->ob_spec;
-
- te->te_thickness=2;
-
- if (wind)
- {
- if (wind==top_w)
- {
- objc_draw(t,dialog_details[dialog].current_focus,1,scrn_x,scrn_y,scrn_w,scrn_h);
- }else{
- dialog_display(dialog);
- }
- }
-
- wind_update(END_UPDATE);
- graf_mouse(M_ON,NULL);
- }
-
- short ted_callback(void)
- {
- OBJECT *t;
- TEDINFO *te;
- Elist *e;
- short curpos,o;
- short rtn;
- short f;
- char *text;
- short c;
-
- rtn=0;
- rsrc_gaddr(0,this_dialog,&t);
-
- te=(TEDINFO*)(t+this_ob)->ob_spec;
-
- text=te->te_ptext;
- curpos=te->te_tmplen;
-
- switch(kc_key)
- {
- case 0x4d00: // Cursor right
- if (curpos<te->te_txtlen-1)
- te->te_tmplen++;
- rtn=1;
- break;
- case 0x4b00: // Cursor left
- if (curpos>0) te->te_tmplen--;
- rtn=1;
- break;
- case 0x4d36: // Shift+Cursor right
- te->te_tmplen=te->te_txtlen-1;
- rtn=1;
- break;
- case 0x4b34: // Shift+Cursor left
- te->te_tmplen=0;
- rtn=1;
- break;
- case 0x5000: // Next field: several characters do this, cursor down
- case 0x0f09: // Tab
- e=event_value[this_dialog];
- for(o=e->object; (e!=NULL)&&(o!=this_ob); )
- {
- o=e->object;
- if (o!=this_ob) { e=e->next; }
- }
- if (o==this_ob)
- {
- e=e->next;
- if (e!=NULL)
- {
- for(o=e->object; (e!=NULL)&&(e->Kcallback==NULL); )
- {
- o=e->object;
- if (e->Kcallback==NULL) { e=e->next; }
- }
- }
- if (e==NULL)
- {
- e=event_value[this_dialog];
- for(o=e->object; (e!=NULL)&&(e->Kcallback==NULL); )
- {
- o=e->object;
- if (e->Kcallback==NULL) { e=e->next; }
- }
- }
- Set_dialog_Kfocus(this_dialog, o);
- }
- rtn=1;
- break;
- case 0x4800: // Cursor up - ignore
- rtn=1;
- break;
- case 0x011b: // Escape - clear field
- for(f=0; f<te->te_txtlen-1; f++) text[f]=' ';
- te->te_tmplen=0;
- rtn=1;
- break;
- case 0x0e08: // Backspace
- if (curpos)
- {
- for(f=curpos-1; f<te->te_txtlen-2; f++) text[f]=text[f+1];
- text[te->te_txtlen-1]=' ';
- te->te_tmplen--;
- }
- rtn=1;
- break;
- case 0x537f: // Delete
- for(f=curpos; f<te->te_txtlen-2; f++) text[f]=text[f+1];
- text[te->te_txtlen-1]=' ';
- rtn=1;
- break;
- }
-
- if ((rtn==0)&&(kc_shstate&(K_CTRL|K_ALT))) rtn=2; // Prevent alt/ctrl from being processed
- // - allow keyboard shortcuts to get through
-
- if (!rtn)
- {
- c=(short)(kc_key&255); // Convert scan code into ASCII character.
- if (c>31)
- {
- text[curpos]=(char)c;
-
- if (curpos<te->te_txtlen-1)
- te->te_tmplen++;
-
- rtn=1;
- }
- }
-
- objc_draw(t,this_ob,0,cr_clip.g_x,cr_clip.g_y,cr_clip.g_w,cr_clip.g_h);
-
- ted_display_cursor();
-
- if (rtn==1)
- return TRUE;
- else
- return FALSE;
- }
-
- short ted_display_cursor(void)
- {
- OBJECT *t;
- TEDINFO *te;
- short curpos,pt[8];
- char *text,c,cc[2];
- short th,lw,cw;
-
- if (dialog_details[this_dialog].current_focus!=this_ob) return TRUE;
-
- rsrc_gaddr(0,this_dialog,&t);
- t+=this_ob;
-
- te=(TEDINFO*)t->ob_spec;
- curpos=te->te_tmplen;
- text=te->te_ptext;
- c=text[curpos];
- text[curpos]='\0';
- vqt_extent(x_handle, text, pt);
- text[curpos]=c;
- th=pt[1]-pt[7];
- lw=pt[2]-pt[0];
- cc[0]=c; cc[1]='\0';
- vqt_extent(x_handle, cc, pt);
- cw=pt[2]-pt[0];
-
- vswr_mode(x_handle,MD_XOR);
-
- pt[0]=cr_clip.g_x+lw+1; pt[1]=cr_clip.g_y+2;
- pt[2]=cr_clip.g_x+lw+cw; pt[3]=cr_clip.g_y+cr_clip.g_h-4;
-
- vsf_color(x_handle, BLACK);
- vsf_style(x_handle, FIS_SOLID);
-
- v_bar(x_handle, pt);
-
- vswr_mode(x_handle,MD_TRANS);
-
- return FALSE;
- }
-
- short ted_mouseclick(void)
- {
- short mb,dummy;
-
- Set_dialog_Kfocus(this_dialog, this_ob);
-
- do { graf_mkstate(&dummy,&dummy,&mb,&dummy); } while (mb);
-
- return TRUE;
- }
-